package com.example.demo.config;


import com.google.common.util.concurrent.RateLimiter;
import org.springframework.stereotype.Component;
@Component
public class MyRateLimiter {
    /**
     * 账号注册限流器 每秒只发出5个令牌
     */

    private RateLimiter accountRegisterRateLimiter = RateLimiter.create(5.0);

    /**
     * 短信发送限流器    每秒只发出3个令牌
     */

    private RateLimiter smsSendRateLimiter = RateLimiter.create(3.0);

    /**
     * 尝试获取令牌，返回尝试结果
     *
     * @return
     */
    public boolean tryAccountRegisterAcquire() {
        return accountRegisterRateLimiter.tryAcquire();
    }

    /**
     * 取令牌，暂时取不到会一直去尝试
     * @return
     */
    public double accountRegisterAcquire() {
        return accountRegisterRateLimiter.acquire();
    }

    /**
     * 尝试获取令牌
     *
     * @return
     */
    public boolean trySmsSendAcquire() {
        return smsSendRateLimiter.tryAcquire();
    }
}
